package com.yc.fruitmarket.biz.impl;

import java.util.List;
import java.util.Map;

import com.yc.fruitmarket.biz.userBiz;
import com.yc.fruitmarket.dao.DBHelper;

public class userBizImpl implements userBiz {
	private DBHelper db = new DBHelper();
	
	//获取所有用户信息
	@Override
	public List<Map<String, String>> getAllUsers() {
		String sql = "select * from user";
		return db.find(sql);
	}	
	@Override
	public int reg(String uname, String pwd, String email, String tel) {
		String sql = "insert into user values(null,?,md5(?),'',?,?,0,0)";
		int reslut = db.doUpdate(sql, uname, pwd, tel, email);
		return reslut;
	}

	@Override
	public boolean checkEmail(String email) {
		String sql = "select * from user where email = ?";
		List<Map<String, String>> list = db.find(sql, email);
		
		return list == null || list.size() <= 0 ? false : true;
	}

	@Override
	public boolean checkTel(String tel) {
		String sql = "select * from user where tel = ?";
		List<Map<String, String>> list = db.find(sql, tel);
		
		return list == null || list.size() <= 0 ? false : true;
	}

	@Override
	public boolean checkUname(String uname) {
		String sql = "select * from user where uname = ?";
		List<Map<String, String>> list = db.find(sql, uname);
		
		return list == null || list.size() <= 0 ? false : true;
	}

	@Override
	public List<Map<String, String>> login(String user, String pwd) {
		String sql = "select * from user where (uname = ? or tel = ? or email = ?) and pwd = md5(?)";
		return db.find(sql, user, user, user, pwd);
	}

	@Override
	public List<Map<String, String>> findAdminInfo() {
		String sql = "select * from user where status = 1";
		return db.find(sql);
	}
	//条件查询用户信息
	@Override
	public List<Map<String, String>> byselect(String uname, String email, String tel, String status, String pagenum) {
		String sql = "select * from user where 1 = 1";
		if(!"".equals(uname)) {
			sql += " and uname like '%"+uname+"%'";
		}
		if(!"".equals(email)) {
			sql += " and email like '%"+email+"%'";
		}
		if(!"".equals(tel)) {
			sql += " and tel like '%"+tel+"%'";
		}
		if(!"所有".equals(status) && !"0".equals(status)) {
			if(Integer.parseInt(status) == 4) {
				sql += " and status != 2";
			}else {
				sql += " and status = "+(Integer.parseInt(status) - 1);
			}
		}
		List<Map<String, String>> list = db.find(sql);
		String a = String.valueOf(list.size());
		 sql += " limit ?,5";
		 List<Map<String, String>> list1 = db.find(sql, (Integer.parseInt(pagenum)-1) * 1);
		 for(Map<String, String> map : list1) {
				map.put("count", a);
			}
		return list1;
	}
	//封禁与解封
	@Override
	public int banner(String uid, int i) {
		String sql = "update user set status = ? where uid = ?";
		return db.doUpdate(sql, i, uid);
	}
	//用户的修改
	@Override
	public int updateUserData(Map<String, String> map, String pic) {
		if("".equals(pic)) {
			String sql = "update user set uname = ?, tel = ?, email = ?, balance = ? where uid = ?";
			return db.doUpdate(sql, map.get("uname"), map.get("tel"), map.get("email"), map.get("balance"), map.get("uid"));
		}else {
			String sql = "update user set uname = ?, pic = ?, tel = ?, email = ?, balance = ? where uid = ?";
			return db.doUpdate(sql, map.get("uname"), pic, map.get("tel"), map.get("email"), map.get("balance"), map.get("uid"));
		}
	}
	//重置密码
	@Override
	public int cz(String uid) {
		String pwd = "a";
		String sql = "update user set pwd = md5(?) where uid = ?";
		return db.doUpdate(sql, pwd, uid);
	}
	//重新获取用户信息
	@Override
	public List<Map<String, String>> user(String uid) {
		String sql = " select * from user where uid = ?";
		return db.find(sql, uid);
	}
	@Override
	public int sendDataToServer(String quans,String uid) {
		String sql = "select * from discountcard where discount = ? and uid = ?";
		List<Map<String, String>> list = db.find(sql, quans, uid);
		int result = 0;
		if(list.size()>0) {
			String update = "update discountcard set disnum = disnum + 1, status = 1 where discount = ? and uid = ?";
			result = db.doUpdate(update, quans, uid);
		}else {
			String insert = "insert into discountcard values(null,?,?,1,1)";
			result = db.doUpdate(insert, quans, uid);
		}
		return result;
	}
	
}
